﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DeloUtils.Migra
{
    class Data
    {
        public String Name;
        public DateTime ExportTime = DateTime.MinValue;
        public DateTime ImportTime = DateTime.MinValue;
        public Int32 ExportISN = 0;
        public Int32 ImportISN = 0;
        public String ExportDUE = string.Empty;
        public String ImportDUE = string.Empty;

        public override String ToString()
        {
            String Result = Name.ToUpper() + ":{";
            if (ExportTime != DateTime.MinValue)
            {
                Result += "[" + ExportTime.ToString() + "]";
            }
            Result += ",";
            if (ExportISN != 0)
            {
                Result += "[" + ExportISN.ToString() + "]";
            }
            Result += ",";
            if (ExportDUE!=string.Empty)
            {
                Result += "[" + ExportDUE + "]";
            }
            Result += "},{";
            if (ImportTime != DateTime.MinValue)
            {
                Result += "[" + ImportTime.ToString() + "]";
            }
            Result += ",";
            if (ImportISN != 0)
            {
                Result += "[" + ImportISN.ToString() + "]";
            }
            Result += ",";
            if (ImportDUE != string.Empty)
            {
                Result += "[" + ImportDUE + "]";
            }
            Result += "}";
            return Result;
        }

        public Data(String Name, DateTime ExportTime, Int32 ExportISN, String ExportDUE)
        {
            this.Name = Name;
            this.ExportTime = ExportTime;
            this.ExportISN = ExportISN;
            this.ExportDUE = ExportDUE;
            this.ImportTime = DateTime.MinValue;
            this.ImportISN = 0;
            this.ImportDUE = string.Empty;
        }

        public Data(String Name, DateTime ExportTime, Int32 ExportISN, String ExportDUE, DateTime ImportTime, Int32 ImportISN, String ImportDUE)
        {
            this.Name = Name;
            this.ExportTime = ExportTime;
            this.ExportISN = ExportISN;
            this.ExportDUE = ExportDUE;
            this.ImportTime = ImportTime;
            this.ImportISN = ImportISN;
            this.ImportDUE = ImportDUE;
        }

        public Data(String Value)
        {
            Int32 EndName = Value.IndexOf(':');
            Name = Value.Substring(0, EndName).Trim();
            Int32 StartExport = Value.IndexOf('{', EndName);
            Int32 EndExport = Value.IndexOf('}', EndName);
            String Export = Value.Substring(StartExport + 1, EndExport - StartExport - 1);
            Int32 StartImport = Value.IndexOf('{', EndExport);
            Int32 EndImport = Value.IndexOf('}', StartImport);
            String Import = Value.Substring(StartImport + 1, EndImport - StartImport - 1);
            String[] ExportList = Export.Split(',');
            String[] ImportList = Import.Split(',');
            String _;
            _ = Strip(ExportList[0]);
            ExportTime = (_ != string.Empty) ? DateTime.Parse(_) : DateTime.MinValue;
            _ = Strip(ExportList[1]);
            ExportISN = (_ != string.Empty) ? Int32.Parse(_) : 0;
            ExportDUE = Strip(ExportList[2]);
            _ = Strip(ImportList[0]);
            ImportTime = (_ != string.Empty) ? DateTime.Parse(_) : DateTime.MinValue;
            _ = Strip(ImportList[1]);
            ImportISN = (_ != string.Empty) ? Int32.Parse(_) : 0;
            ImportDUE = Strip(Strip(ImportList[2]));
        }

        private String Strip(String Value)
        {
            if (Value == string.Empty) return string.Empty;
            if (Value.Trim() == "") return string.Empty;
            Int32 Start = Value.IndexOf('[');
            Int32 End = Value.IndexOf(']');
            return Value.Substring(Start + 1, End - Start - 1).Trim();
        }

        public void SaveDB(LocalDBCompact DB)
        {
            if (DB!= null) DB.Save(this);
        }
    }
}
